﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample82
{
    [ModulePriority(ModulePriority.Assets)]
    public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
    {
        private readonly ILogger<AssetsModule> _Logger;

        public AssetsModule(ILogger<AssetsModule> logger)
        {
            _Logger = logger;
        }

        public  VulkanRenderWapper RenderWapper { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {

            try
            {
                var basePath = AppContext.BaseDirectory;
                var textures = Path.Combine(basePath, "Assets", "Textures");

                graphicsManager.TryGetRenderGroup("EarthGroup", out IRenderGroup earthGroup);
                graphicsManager.TryGetRenderGroup("MoonGroup", out IRenderGroup moonGroup);
                graphicsManager.TryGetRenderGroup("StarGroup", out IRenderGroup starGroup);
                graphicsManager.TryGetRenderGroup("CloudGroup", out IRenderGroup cloudGroup);
                var lDevice = graphicsManager.VulkanDevice;

                ISEADocument document = new ISEADocument();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "test82.isea"));
                var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);
                var light = graphicsManager.CreateParallelLight<RenderObject>("main", new Vector3(0, 100, 100), new LightStrength(0.9f, 0.9f, 0.5f, 0.8f));

                var earthArchetype = ContextManager.CreateArchetype(new TransformComponent(), new EarthComponent());
                var moonArchetype = ContextManager.CreateArchetype(new TransformComponent(), new MoonComponent());
                var starArchetype = ContextManager.CreateArchetype(new TransformComponent(), new StarComponent());
                var archetype2 = ContextManager.CreateArchetype(new LightComponent());

                var earth = renders.Where(x => x.Name == "Earth").First();
                var cloud = renders.Where(x => x.Name == "Cloud").First();
                var moon = renders.Where(x => x.Name == "Moon").First();
                var smallSky = renders.Where(x => x.Name == "SmallSky").First();
                var bigSly = renders.Where(x => x.Name == "BigSky").First();
                ContextManager.CreateContexts(1, earthArchetype, earthGroup, earth);
                ContextManager.CreateContexts(1, earthArchetype, cloudGroup, cloud);
                ContextManager.CreateContexts(1, moonArchetype, moonGroup, moon);
                ContextManager.CreateContexts(1, starArchetype, starGroup, smallSky);
                ContextManager.CreateContexts(1, starArchetype, starGroup, bigSly);
                ContextManager.CreateContexts(1, archetype2, earthGroup, light);
                ContextManager.CreateContexts(1, archetype2, moonGroup, light);
                ContextManager.CreateContexts(1, archetype2, cloudGroup, light);
            }
            catch (Exception e)
            {
                _Logger.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
